package com.ilearn.rest.controller;

/**
 * @Project: cmcchq
 * @Description:
 * @Version 1.0
 * @Throws :
 * @Author: <li>2019/6/27 lyndon Create 1.0
 * @Copyright (c)2019-2019 版权所有
 * @Modified By:
 */
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.ilearn.rest.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;

@RestController
public class AsyncController {

    private static Logger log = LoggerFactory.getLogger(AsyncController.class);

    @Autowired
    private AsyncService service;

    @RequestMapping(value = "/testAsynch", method = RequestMethod.GET)
    public void testAsynch() throws InterruptedException, ExecutionException
    {
        log.info("Main Start " + Thread.currentThread().getName());

        CompletableFuture<List<String>> employeeAddress = service.getEmployeeAddress();
        CompletableFuture<List<String>> employeeName = service.getEmployeeName();
        CompletableFuture<List<String>> employeePhone = service.getEmployeePhone();

        // 等待每个异步调用都完成
        CompletableFuture.allOf(employeeAddress, employeeName, employeePhone).join();

        log.info("EmployeeAddress--> " + employeeAddress.get());
        log.info("EmployeeName--> " + employeeName.get());
        log.info("EmployeePhone--> " + employeePhone.get());
        log.info("Main End " + Thread.currentThread().getName());
    }


    @RequestMapping(value = "/testAsynch2", method = RequestMethod.GET)
    public Callable<String> testAsynch2() throws InterruptedException, ExecutionException
    {
        long start = System.currentTimeMillis();
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.info("Main Start " + Thread.currentThread().getName());

                CompletableFuture<List<String>> employeeAddress = service.getEmployeeAddress();
                CompletableFuture<List<String>> employeeName = service.getEmployeeName();
                CompletableFuture<List<String>> employeePhone = service.getEmployeePhone();

                // 等待每个异步调用都完成
                CompletableFuture.allOf(employeeAddress, employeeName, employeePhone).join();

                log.info("EmployeeAddress--> " + employeeAddress.get());
                log.info("EmployeeName--> " + employeeName.get());
                log.info("EmployeePhone--> " + employeePhone.get());
                log.info("Main End " + Thread.currentThread().getName());
                long times = System.currentTimeMillis() - start;
                return "task/task - " + String.valueOf(times);
            }
        };


    }


    @RequestMapping(value = "/asyncTimeout", method = RequestMethod.GET)
    public WebAsyncTask<String> asyncTimeout() throws InterruptedException, ExecutionException
    {
        long start = System.currentTimeMillis();
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.info("Main Start " + Thread.currentThread().getName());

                CompletableFuture<List<String>> employeeAddress = service.getEmployeeAddress();
                CompletableFuture<List<String>> employeeName = service.getEmployeeName();
                CompletableFuture<List<String>> employeePhone = service.getEmployeePhone();

                // 等待每个异步调用都完成
                CompletableFuture.allOf(employeeAddress, employeeName, employeePhone).join();

                log.info("EmployeeAddress--> " + employeeAddress.get());
                log.info("EmployeeName--> " + employeeName.get());
                log.info("EmployeePhone--> " + employeePhone.get());
                log.info("Main End " + Thread.currentThread().getName());
                long times = System.currentTimeMillis() - start;
                Thread.sleep(8000);
                return "task/task - " + String.valueOf(times);
            }
        };

        return new WebAsyncTask<String>(10000, callable);
    }
}